package com.leyou.item.comsume;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.service.BrandService;
import com.leyou.pojo.dto.BrandDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")
public class BrandComsume {

    @Autowired
    BrandService brandService;
    /**
     * 根据品牌id集合查询品牌集合
     */
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryBrandsById(@RequestParam("ids")List<Long> ids) {
        return ResponseEntity.ok(brandService.queryBrandsById(ids));
    }

    /**
     * 根据分类id查询品牌
     * @param id 分类id
     * @return
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandsByCategoryId(@RequestParam("id") Long id) {
        return ResponseEntity.ok(brandService.queryBrandsByCategoryId(id));
    }

    /**
     * 分页查询品牌
     * @param page 当前页
     * @param rows 每页大小
     * @param key 查询条件
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>> queryBrandsPage(
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows,
            @RequestParam(value = "key",required = false) String key
    ) {
        return ResponseEntity.ok(brandService.queryBrandsPage(page,rows,key));
    }

    /**
     * 根据id查询品牌
     * @param id 品牌id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id") String id) {
        return ResponseEntity.ok(brandService.queryBrandById(id));
    }

    /**
     * 新增品牌
     * @param categoryIds
     * @param brandDTO
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> addBrand(
            @RequestParam("categoryIds") List<String> categoryIds,
            BrandDTO brandDTO
    ) {
        brandService.addBrand(brandDTO,categoryIds);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 更新品牌
     * @param categoryIds
     * @param brandDTO
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateBrand(
            @RequestParam("categoryIds") List<String> categoryIds,
            BrandDTO brandDTO
    ) {
        brandService.updateBrand(categoryIds,brandDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据id删除品牌
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteBrandById(@PathVariable("id") Long id) {
        brandService.deleteBrandById(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
